using GeneralMinioApi.Services;
using Microsoft.OpenApi.Models;
using Minio;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "GeneralMinioApi", Version = "v1" });

    // 1. 定义三个独立的安全方案
    //c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
    //{
    //    Name = "X-Api-Key",
    //    In = ParameterLocation.Header,
    //    Type = SecuritySchemeType.ApiKey,
    //    Description = "API密钥"
    //});

    c.AddSecurityDefinition("Timestamp", new OpenApiSecurityScheme
    {
        Name = "X-Timestamp",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
        Description = "请求时间戳（Unix秒）"
    });

    c.AddSecurityDefinition("Signature", new OpenApiSecurityScheme
    {
        Name = "X-Signature",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
        Description = "HMAC-SHA256签名"
    });

    // 2. 将这三个方案同时应用到需要保护的API上
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        //{
        //    new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "ApiKey" } },
        //    new string[] {}
        //},
        {
            new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Timestamp" } },
            new string[] {}
        },
        {
            new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Signature" } },
            new string[] {}
        }
    });
});

var minioConfig = builder.Configuration.GetSection("Minio");
var endpoint = minioConfig["Endpoint"];
var accessKey = minioConfig["AccessKey"];
var secretKey = minioConfig["SecretKey"];
builder.Services.AddSingleton<IMinioClient>(sp =>
    new MinioClient()
        .WithEndpoint(endpoint)
        .WithCredentials(accessKey, secretKey)
        .WithSSL(false)
        .Build());

builder.Services.AddSingleton<MinioService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
//}

app.UseAuthorization();

app.MapControllers();

app.Run();
